/* EasyWay Game Engine
 * Copyright (C) 2007 Daniele Paggi.
 *  
 * Written by: 2007 Daniele Paggi<dshnt@hotmail.com>
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.easyway.editor.commands;

import java.util.ArrayList;

import org.easyway.editor.commands.intrefaces.ICommand;

public class CommandList {

	static ArrayList<ICommand> objlistToCreate = new ArrayList<ICommand>(10);

	static ArrayList<ICommand> objlist = new ArrayList<ICommand>(10);

	static ArrayList<ICommand> destroyList = new ArrayList<ICommand>(10);

	static public void render() {

		// loop all
		for (ICommand obj : objlist) {
			if (obj.isDestroyed()) {
				destroyList.add(obj);
				continue;
			}
			obj.render();
			if (obj.isDestroyed()) {
				destroyList.add(obj);
				continue;
			}
		}

		// destroy all
		for (int i = destroyList.size() - 1; i >= 0; --i) {
			objlist.remove(destroyList.get(i));
			destroyList.remove(i);
		}
	}

	static public void loop() {
		synchronized (objlistToCreate) {

			for (ICommand obj : objlistToCreate) {
				obj.creation();
				objlist.add(obj);
			}
			objlistToCreate.clear();
			// loop all
			for (ICommand obj : objlist) {
				if (obj.isDestroyed()) {
					destroyList.add(obj);
					continue;
				}
				obj.loop();
				if (obj.isDestroyed()) {
					destroyList.add(obj);
					continue;
				}
			}
		}

		// destroy all
		for (int i = destroyList.size() - 1; i >= 0; --i) {
			objlist.remove(destroyList.get(i));
			destroyList.remove(i);
		}

	}

}
